package com.iwdnb.gkgz.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.bmnf.common.model.result.Result;
import com.iwdnb.bmnf.common.utils.EnumUtils;
import com.iwdnb.gkgz.application.kline.ShapeEnum;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockDayDataRepository;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockRepository;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * K线形态
 */
@RestController
@RequestMapping("/api/stock/shape")
public class StockShapeController {

    @Autowired
    private StockRepository stockRepository;
    @Autowired
    private StockDayDataRepository stockDayDataRepository;

    /**
     * 获取股票指定形态的日线数据
     *
     * @param code
     * @return
     */
    @GetMapping(value = "/getShapeStockDayList")
    public Result<List<String>> getShapeStockDayList(String code, String shapes) {
        String startDateStr = "2010-01-01 00:00:00";
        String endDateStr = DateUtil.formatDateTime(new Date());
        List<StockDayData> stockDayDataList = stockDayDataRepository.queryAllByDateRange(code, startDateStr,
            endDateStr);
        String[] shapeArray = shapes.split(",");
        List<String> matchDateList = new ArrayList<>();
        for (String shape : shapeArray) {
            ShapeEnum shapeEnum = EnumUtils.getEnumObjectByCode(ShapeEnum.class, shape);
            List<StockDayData> matchList = shapeEnum.getShape().getMatchData(code,stockDayDataList, matchDateList);
            List<String> dateList = matchList.stream().map(StockDayData::getDate).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(matchDateList)) {
                matchDateList.addAll(dateList);
            } else {
                //求交集
                matchDateList.retainAll(dateList);
            }
        }
        return Result.getSuccessResult(matchDateList);
    }


}
